package com.loongtech.bi.action.bipay;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.pay.ActiveUser5LevelFirstAddcashManager;
import com.loongtech.bi.manager.pay.HabitPayManager;
import com.loongtech.bi.manager.pay.HabitPayManager.FirstPayCount;
import com.loongtech.bi.manager.pay.HabitPayManager.FirstPayLevel;
import com.loongtech.bi.manager.pay.HabitPayManager.FirstPayPlayTime;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 付费分析 —付费习惯Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/pay/habitPay")
public class HabitPayBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private List<FirstPayCount> firstPayCountData;  // 首付金额数据
	private ChartUIData firstPayCountChart;  // 首付金额图表
	
	private List<FirstPayLevel> firstPayLevelData;  // 首付等级数据
	private ChartUIData firstPayLevelChart;  // 首付等级图表
    private ChartUIData firstPayLevelChart1;  // 首付等级图表
	
	private List<FirstPayPlayTime> firstPayPlayTimeData;  // 首付金额数据
	private ChartUIData firstPayPlayTimeChart;  // 首付金额图表
	
	private String levleName = "1-5等级";  // 首付等级选中的等级数
	private Map <String,String> firstpayLevelMap;  // 首付等级可选的等级数
	private String firstPayLevelOption = "userCount";  // 首付游戏时长图表切换项（userCount=人数,payCount=金额），默认为人数
	
	private String firstPayPlayTimeOption = "days";  // 首付游戏时长图表切换项（days=天数,time=累计时长），默认为天数
	
	@Autowired
	private HabitPayManager habitPayManager;
	@Autowired
	private ActiveUser5LevelFirstAddcashManager activeUser5LevelFirstAddcashManager;
	
    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
	    //设置等级段
        setFirstPaylevelMap();
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
        //设置等级段
		setFirstPaylevelMap();

        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            firstPayCountData = habitPayManager.getFirstPayCountData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);

            if (firstPayCountData != null && firstPayCountData.size() > 0) {
                firstPayCountChart = new ChartUIData();
                firstPayCountChart.setJsonType(ChartType.BAR2.type);
                firstPayCountChart.setJsonStr(new ChartTool(firstPayCountData, FirstPayCount.class, "").loadData(ChartType.BAR.type));
            } else {
                firstPayCountChart = null;
            }

            firstPayLevelData = habitPayManager.getFirstPayLevelData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
            Collections.sort(firstPayLevelData, habitPayManager.new FirstPayLevel().comparatorLevel);
            if (firstPayLevelData != null && firstPayLevelData.size() > 0) {
                firstPayLevelChart = new ChartUIData();
                firstPayLevelChart.setJsonType(ChartType.BAR2.type);
                firstPayLevelChart.setJsonStr(new ChartTool(firstPayLevelData, FirstPayLevel.class, "userCount").loadData(ChartType.BAR.type));

                firstPayLevelChart1 = new ChartUIData();
                firstPayLevelChart1.setJsonType(ChartType.BAR2.type);
                firstPayLevelChart1.setJsonStr(new ChartTool(firstPayLevelData, FirstPayLevel.class, "payCount").loadData(ChartType.BAR.type));
            } else {
                firstPayLevelChart = null;
                firstPayLevelChart1 = null;
            }

            firstPayPlayTimeData = habitPayManager.getFirstPayPlayTimeData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
            if (firstPayPlayTimeData != null && firstPayPlayTimeData.size() > 0) {
                firstPayPlayTimeChart = new ChartUIData();
                firstPayPlayTimeChart.setJsonType(ChartType.BAR2.type);
                firstPayPlayTimeChart.setJsonStr(new ChartTool(firstPayPlayTimeData, FirstPayPlayTime.class, "").loadData(ChartType.BAR.type));
            } else {
                firstPayPlayTimeChart = null;
            }

            result.put("res", "0");
            result.put("firstPayCountChart", firstPayCountChart);
            result.put("firstPayLevelChart", firstPayLevelChart);
            result.put("firstPayLevelChart1", firstPayLevelChart1);
            result.put("firstPayPlayTimeChart", firstPayPlayTimeChart);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}
	
	//设置等级map
	private void setFirstPaylevelMap() {
		firstpayLevelMap = new LinkedHashMap<>();
		
		List<Object[]> list = activeUser5LevelFirstAddcashManager.getFirstAddcashLevelNameByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		
		int index = 0;
		
		for(Object o :list) {
			String level_name = String.valueOf(o);
			firstpayLevelMap.put(level_name, level_name);
			
			if(index == 0) {
				levleName = level_name;
			}
			
			index ++;
		}
	}
	
	// 首付等级分布切换等级分组
	/*public void handleFirstPayLevelChange() {
		firstPayLevelChart = null;
		
		firstPayLevelData = habitPayManager.getFirstPayLevelData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,levleName);
		if (firstPayLevelData != null && firstPayLevelData.size() > 0) {
			firstPayLevelChart = new ChartUIData();
			firstPayLevelChart.setJsonType(ChartType.BAR2.type);
			firstPayLevelChart.setJsonStr(new ChartTool(firstPayLevelData, FirstPayLevel.class, firstPayLevelOption).loadData(ChartType.BAR.type));
		}
	}*/

	// 首付游戏时长切换图表项调用的方法
	public void selectedFirstPayPlayTimeOption() {
		firstPayPlayTimeChart = null;
		
		firstPayPlayTimeData = habitPayManager.getFirstPayPlayTimeData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		if (firstPayPlayTimeData != null && firstPayPlayTimeData.size() > 0) {
			firstPayPlayTimeChart = new ChartUIData();
			firstPayPlayTimeChart.setJsonType(ChartType.BAR2.type);
			firstPayPlayTimeChart.setJsonStr(new ChartTool(firstPayPlayTimeData, FirstPayPlayTime.class, "").loadData(ChartType.BAR.type));
		}
	}

    @Override
    public void search() {

    }

    @Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public List<FirstPayCount> getFirstPayCountData() {
		return firstPayCountData;
	}

	public void setFirstPayCountData(List<FirstPayCount> firstPayCountData) {
		this.firstPayCountData = firstPayCountData;
	}

	public ChartUIData getFirstPayCountChart() {
		return firstPayCountChart;
	}

	public void setFirstPayCountChart(ChartUIData firstPayCountChart) {
		this.firstPayCountChart = firstPayCountChart;
	}

	public List<FirstPayPlayTime> getFirstPayPlayTimeData() {
		return firstPayPlayTimeData;
	}

	public void setFirstPayPlayTimeData(List<FirstPayPlayTime> firstPayPlayTimeData) {
		this.firstPayPlayTimeData = firstPayPlayTimeData;
	}

	public ChartUIData getFirstPayPlayTimeChart() {
		return firstPayPlayTimeChart;
	}

	public void setFirstPayPlayTimeChart(ChartUIData firstPayPlayTimeChart) {
		this.firstPayPlayTimeChart = firstPayPlayTimeChart;
	}

	public String getFirstPayPlayTimeOption() {
		return firstPayPlayTimeOption;
	}

	public void setFirstPayPlayTimeOption(String firstPayPlayTimeOption) {
		this.firstPayPlayTimeOption = firstPayPlayTimeOption;
	}

	public List<FirstPayLevel> getFirstPayLevelData() {
		return firstPayLevelData;
	}

	public void setFirstPayLevelData(List<FirstPayLevel> firstPayLevelData) {
		this.firstPayLevelData = firstPayLevelData;
	}

	public ChartUIData getFirstPayLevelChart() {
		return firstPayLevelChart;
	}

	public void setFirstPayLevelChart(ChartUIData firstPayLevelChart) {
		this.firstPayLevelChart = firstPayLevelChart;
	}
	
	public String getLevleName() {
		return levleName;
	}

	public void setLevleName(String levleName) {
		this.levleName = levleName;
	}

	public Map<String, String> getFirstpayLevelMap() {
		return firstpayLevelMap;
	}

	public void setFirstpayLevelMap(Map<String, String> firstpayLevelMap) {
		this.firstpayLevelMap = firstpayLevelMap;
	}

	public String getFirstPayLevelOption() {
		return firstPayLevelOption;
	}

	public void setFirstPayLevelOption(String firstPayLevelOption) {
		this.firstPayLevelOption = firstPayLevelOption;
	}

    public ChartUIData getFirstPayLevelChart1() {
        return firstPayLevelChart1;
    }

    public void setFirstPayLevelChart1(ChartUIData firstPayLevelChart1) {
        this.firstPayLevelChart1 = firstPayLevelChart1;
    }
}
